package demoMod.plugins.mod.sts.inspections;

import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;

public class InspectSpireInitializer extends AbstractBaseJavaLocalInspectionTool {
    @Override
    public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
        JavaElementVisitor visitor = new JavaElementVisitor() {
            @Override
            public void visitClass(PsiClass modClass) {
                for (PsiAnnotation annotation : modClass.getAnnotations()) {
                    if (annotation.hasQualifiedName("com.evacipated.cardcrawl.modthespire.lib.SpireInitializer")) {
                        boolean hasInitializeMethod = false;
                        for (PsiMethod method : modClass.getAllMethods()) {
                            if (method.getName().equals("initialize") &&
                                    method.getReturnType().equals(PsiType.VOID) &&
                                    method.getModifierList().hasModifierProperty("static") &&
                                    method.getModifierList().hasModifierProperty("public") &&
                                    method.getParameterList().isEmpty()
                            ) {
                                hasInitializeMethod = true;
                                break;
                            }
                        }
                        if (!hasInitializeMethod) {
                            holder.registerProblem(modClass, "缺少方法：initialize", new AddInitializeMethodQuickFix());
                        }
                    }
                }
            }
        };
        return visitor;
    }
}
